package com.windy.myvideo;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;

import com.windy.myvideo.model.VideoInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dingfeng on 2016/9/16.
 */
public class LocalVideoActivity extends AppCompatActivity {

    private GridView mGridView;

    private List<VideoInfo> mVideoList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_video);
        mGridView = (GridView) findViewById(R.id.gridView);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                VideoInfo video = mVideoList.get(position);
                String path = video.mPath;
                Intent intent = new Intent();
                intent.setClass(LocalVideoActivity.this, MyPlayerActivity.class);
                intent.setData(Uri.parse(path));
                startActivity(intent);
            }
        });
        new GetVideoTask().execute();
    }

    class GetVideoTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            getLocalVideo();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            VideoAdapter adapter = new VideoAdapter(LocalVideoActivity.this, mVideoList);
            mGridView.setAdapter(adapter);
        }
    }

    String proj[] = {
            MediaStore.Video.Media.DISPLAY_NAME,//视频的名字
            MediaStore.Video.Media.SIZE,//大小
            MediaStore.Video.Media.DURATION,//长度
            MediaStore.Video.Media.DATA,//播放地址
    };

    private void getLocalVideo() {
        mVideoList.clear();
        ContentResolver cr = getContentResolver();
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//有sd卡的情况
            Cursor cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
            while (cursor.moveToNext()) {
                String name = cursor.getString(0);//得到视频的名字
                long size = cursor.getLong(1);//得到视频的大小
                long duration = cursor.getLong(2);//得到视频的时间长度
                String data = cursor.getString(3);//得到视频的路径，可以转化为uri进行视频播放

                VideoInfo videoInfo = new VideoInfo();
                videoInfo.setName(name);
                videoInfo.setPath(data);
                videoInfo.setDuration(duration);
                videoInfo.setSize(size);
                videoInfo.setThumb(data);

                mVideoList.add(videoInfo);
            }
        }
        //不论是否有sd卡都要查询手机内存
        Cursor cursor = cr.query(MediaStore.Video.Media.INTERNAL_CONTENT_URI, proj, null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(0);//得到视频的名字
            long size = cursor.getLong(1);//得到视频的大小
            long duration = cursor.getLong(2);//得到视频的时间长度
            String data = cursor.getString(3);//得到视频的路径，可以转化为uri进行视频播放

            VideoInfo videoInfo = new VideoInfo();
            videoInfo.setName(name);
            videoInfo.setPath(data);
            videoInfo.setDuration(duration);
            videoInfo.setSize(size);
            videoInfo.setThumb(data);

            mVideoList.add(videoInfo);
        }
    }
}
